package com.example.warehouse.bus.dao.persist.repository.impl;

import com.example.warehouse.bus.dao.persist.mapper.BusGoodsMapper;
import com.example.warehouse.bus.dao.persist.repository.IBusGoodsRepository;
import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.entity.BusGoods;
import com.example.warehouse.bus.pojo.vo.BusCustomerStandardVO;
import com.example.warehouse.bus.pojo.vo.BusGoodsStandardVO;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Linger
 * @date 2023-07-07 8:57
 * @desciption: 处理商品数据的实体类
 */
@Slf4j
@Repository
public class BusGoodsRepositoryImpl implements IBusGoodsRepository {
    @Autowired
    private BusGoodsMapper busGoodsMapper;

    public BusGoodsRepositoryImpl() {
        log.info("创建控制器对象: BusGoodsRepositoryImpl");
    }

    /**
     * 新增商品
     *
     * @param busGoods 商品数据
     * @return受影响的行数
     */
    @Override
    public int insert(BusGoods busGoods) {
        log.debug("开始执行【新增商品】的数据访问，参数：{}", busGoods);
        return busGoodsMapper.insert(busGoods);
    }

    @Override
    public int countByGoodsNameAndProviderId(String goodsName, Long providerId) {
        log.debug("开始执行【统计供应id和商品名组合数量】的数据访问，参数：{},{}", goodsName, providerId);
        return busGoodsMapper.countByGoodsNameAndProviderId(goodsName, providerId);
    }

    @Override
    public int countByPromitCode(String promitCode) {
        log.debug("开始执行【统计批准文号数量】的数据访问，参数：{}", promitCode);
        return busGoodsMapper.countByPromitCode(promitCode);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除商品】的数据访问，参数：{}", id);
        return busGoodsMapper.deleteById(id);
    }

    @Override
    public BusGoodsStandardVO getStandardById(Long id) {
        log.debug("通过id查询所有商品信息,参数：{}", id);
        return busGoodsMapper.getStandardByGoodsId(id);
    }

    @Override
    public int update(BusGoods busGoods) {
        log.debug("开始执行【修改商品】的数据访问，参数：{}", busGoods);
        return busGoodsMapper.updateById(busGoods);
    }

    @Override
    public BusGoodsStandardVO getStandardByGoodsId(Long id) {
        log.debug("通过id查询所有商品信息,参数：{}",id);
        return busGoodsMapper.getStandardByGoodsId(id);
    }

    @Override
    public PageData<BusGoodsStandardVO> queryByKeyWord(Integer pageNum, Integer pageSize,String goodsName, Long providerId, String promitCode, String producePlace, String productCode, String description) {
        log.debug("模糊查询,{},{},{},{},{},{}");
        PageHelper.startPage(pageNum, pageSize);
        List<BusGoodsStandardVO> list = busGoodsMapper.queryByKeyWord(goodsName, providerId, promitCode, producePlace, productCode, description);
        PageInfo<BusGoodsStandardVO> pageInfo = new PageInfo<>(list);
        PageData<BusGoodsStandardVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;

    }

    @Override
    public PageData<BusGoods> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询商品列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusGoods> list = busGoodsMapper.list();
        PageInfo<BusGoods> pageInfo = new PageInfo<>(list);
        PageData<BusGoods> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<BusGoods> listAsc(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【升序查询商品列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusGoods> list = busGoodsMapper.listAsc();
        PageInfo<BusGoods> pageInfo = new PageInfo<>(list);
        PageData<BusGoods> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<BusGoods> listDesc(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【降序查询商品列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusGoods> list = busGoodsMapper.listDesc();
        PageInfo<BusGoods> pageInfo = new PageInfo<>(list);
        PageData<BusGoods> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int updateByGoodsId(BusGoods user) {
        log.debug("开始执行【更新商品】的数据访问，参数：{}", user);
        return busGoodsMapper.updateById(user);
    }

}
